package com.grq.mis.controller;

import cn.hutool.core.lang.Validator;
import com.grq.mis.entity.Department;
import com.grq.mis.entity.Item;
import com.grq.mis.exception.BusinessException;
import com.grq.mis.exception.ResultEnum;
import com.grq.mis.model.sys.Me;
import com.grq.mis.service.DepartmentService;
import com.grq.mis.service.ItemService;
import com.grq.mis.service.OrderService;
import org.beetl.sql.core.page.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/center")
public class CenterController extends BaseController {

    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private ItemService itemService;
    @Autowired
    private OrderService orderService;

    @GetMapping("/index")
    public String centerIndex(Model model) {

        List<Department> departments = departmentService.createLambdaQuery().desc(Department::getId).limit(1, 4).select();
        model.addAttribute("departments", departments);

        List<Item> items = itemService.createLambdaQuery().desc(Item::getId).limit(1, 4).select();
        model.addAttribute("items", items);

        return "center/index.html";
    }

    @GetMapping("/item")
    public String item(Model model, @RequestParam Integer id) {

        Item item = itemService.single(id);
        if (item == null) {
            throw new BusinessException(ResultEnum.ERROR, "商品信息不存在");
        }

        model.addAttribute("item", item);

        return "center/item.html";
    }

    @GetMapping("/buyForm")
    public String buyForm(Model model, @RequestParam Integer itemId) {

        Item item = itemService.single(itemId);
        if (item == null) {
            throw new BusinessException(ResultEnum.ERROR, "商品信息不存在");
        }

        model.addAttribute("item", item);

        return "center/buy.html";
    }

    @PostMapping("/buy")
    public String buy(RedirectAttributes attr, @RequestParam Long itemId, @RequestParam Integer count, @RequestParam String name, @RequestParam String phone, String address) {

        if (count < 1) {
            throw new BusinessException(ResultEnum.ERROR, "数量最小为1");
        }
        if (Validator.isEmpty(name)){
            throw new BusinessException(ResultEnum.PARAM_ERROR,"姓名不能为空");
        }
        if (Validator.isEmpty(phone)){
            throw new BusinessException(ResultEnum.PARAM_ERROR,"联系电话不能为空");
        }

        Me me = me();
        orderService.buy(itemId,count,name,phone,address,me);

        return pageSuccess(attr,"/order/page",ResultEnum.SUCCESS.getCode(),"下单成功");
    }

    @GetMapping("/allItem")
    public String allItem(
            Model model,
            @RequestParam(defaultValue = "1") long page,
            @RequestParam(defaultValue = "20") int size,
            String key
    ) {

        // 带入参数
        Map<String, Object> paras = new HashMap<>();
        if (!StringUtils.isEmpty(key)) {
            paras.put("key", "%" + key.trim() + "%");
        }

        PageResult<Item> result = itemService.page("item.all", paras, page, size);
        model.addAttribute("data", result);

        model.addAttribute("key", key);

        return "center/page.html";
    }


}
